package com.loong.common.load.utils;

import com.alibaba.fastjson.JSONObject;
import com.loong.common.exception.GlobalException;
import com.loong.common.load.constant.OrderUrl;
import com.loong.common.http.OKHttpUtil;
import com.loong.common.load.param.OrderParam;
import com.loong.common.load.param.OrderOrBalanceQueryParam;
import com.loong.common.load.param.ReceiptQueryParam;
import com.loong.common.load.result.*;
import com.loong.common.utils.Md5Utils;
import com.loong.common.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LiuQun
 * @date 2020-10-06 15:58
 */
@Slf4j
@Component
public class PhoneChargeUtil {

    @Resource
    private OKHttpUtil okHttpUtil;


    /**
     * 下单接口
     * @param param 订单参数
     * @return 订单结果
     */
    public OrderResult createOrder(OrderParam param) {
        Map<String, String> map = new HashMap<>();
        if (ObjectUtils.isNotEmpty(param)) {
            map.put("uid", param.getUid());
            map.put("pcode", param.getPcode());
            map.put("phone", param.getPhone());
            map.put("money", param.getMoney().toString());
            map.put("orderId",param.getOrderId());
            //auth =  MD5(uid + pcode + phone + money + orderId + (key) )
            String source = param.getUid() + param.getPcode() + param.getPhone() + param.getMoney() + param.getOrderId() + param.getAuthKey();
            map.put("auth", Md5Utils.md5(source));
        }
        String result = okHttpUtil.post(OrderUrl.CREATE_URL, map);
        log.debug("Result==============>{}",result);
        BaseResult baseResult = JSONObject.parseObject(result, BaseResult.class);
        log.debug("BaseResult========>{}",baseResult);
        if (baseResult.isSuccess()){
              return baseResult.getResult().toJavaObject(OrderResult.class);
        }else {
            throw new GlobalException(baseResult.getMessage());
        }
    }

    /**
     * 订单查询接口
     * @param param 订单查询参数
     * @return 订单查询结果
     */
    public OrderQueryResult queryOrder(OrderOrBalanceQueryParam param){
        Map<String, String> map = new HashMap<>();
        String orderId = "XSL"+ UUIDUtils.getUUID();
        if (param != null) {
            map.put("uid", param.getUid());
            map.put("orderId", orderId);
            //auth =  MD5(uid + orderId + (key) )
            String source = param.getUid() + orderId + param.getAuthKey();
            map.put("auth", Md5Utils.md5(source));
        }
        String result = okHttpUtil.post(OrderUrl.ORDER_QUERY_URL, map);
        log.debug("result====>{}",result);
        BaseResult baseResult = JSONObject.parseObject(result, BaseResult.class);
        if (baseResult.isSuccess()){
            return baseResult.getResult().toJavaObject(OrderQueryResult.class);
        }else {
            throw new GlobalException(baseResult.getMessage());
        }
    }
    /**
     * TODO 充值状态回调接口还没写
     */

    /**
     * 代理商余额查询接口
     * @param param 查询参数
     * @return 返回的结果
     */
    public AccountBalanceQueryResult queryBalance(OrderOrBalanceQueryParam param){
        Map<String, String> map = new HashMap<>();
        if (param != null) {
            map.put("uid", param.getUid());
            map.put("timestamp", param.getTimestamp().toString());
            //auth =  MD5(uid + timestamp + (key) )
            String source = param.getUid() + param.getTimestamp() + param.getAuthKey();
            map.put("auth", Md5Utils.md5(source));
        }
        String result = okHttpUtil.post(OrderUrl.GET_BALANCE_URL, map);
        log.debug("result====>{}",result);
        BaseResult baseResult = JSONObject.parseObject(result, BaseResult.class);
        if (baseResult.isSuccess()){
            return baseResult.getResult().toJavaObject(AccountBalanceQueryResult.class);
        }else {
            throw new GlobalException(baseResult.getMessage());
        }
    }

    /**
     * 代理商回执查询
     * @param param 回执查询参数
     * @return 回执结果
     */
    //TODO 这个接口测试时没反应，是不是文档中的url有问题。
    public ReceiptResult queryReceipt(ReceiptQueryParam param){
       String url = OrderUrl.RECEIPT_QUERY_URL+"?phone"+param.getPhone()
                        +"&startTime"+param.getStartTime()+"&endTime"+param.getEndTime();
        String result = okHttpUtil.get(url);
        BaseResult baseResult = JSONObject.parseObject(result, BaseResult.class);
        if (baseResult.isSuccess()){
            return baseResult.getResult().toJavaObject(ReceiptResult.class);
        }else {
            throw new GlobalException(baseResult.getMessage());
        }
    }
}
